﻿#define SAMEDOMAIN

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Reflection;
using System.IO;
//using System.Runtime.Remoting;
//using System.Runtime.Remoting.Channels;
//using System.Runtime.Remoting.Channels.Ipc;
//using ANGFSessionWrapper;

namespace ANGFSessionWrapper
{
    public class MySession: IDisposable
    {
        //public List<QueueBase> RenderingQueue = new List<QueueBase>();
        public int SimpleMenuResult { 
            get { return WrappedWorkerAppDomain.GetSimpleMenuResult(); }
            set { WrappedWorkerAppDomain.SetSimpleMenuResult(value); }
        }
        public string SimpleStringResult { 
            get { return WrappedWorkerAppDomain.GetSimpleStringResult(); } 
            set { WrappedWorkerAppDomain.SetSimpleStringResult(value); } 
        }
        public AppDomain WorkerAppDomain { get; set; }
        private WrapSession wrappedWorkerAppDomain;
        public WrapSession WrappedWorkerAppDomain
        {
            get { return wrappedWorkerAppDomain; }
            private set { wrappedWorkerAppDomain = value; }
        }
        public MySession(string path)
        {
#if DEBUG && SAMEDOMAIN
            WrappedWorkerAppDomain = new WrapSession();
#else
            AppDomainSetup ads = new AppDomainSetup();
            ads.ApplicationBase = path;
            ads.DisallowBindingRedirects = false;
            ads.DisallowCodeDownload = true;
            ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            WorkerAppDomain = AppDomain.CreateDomain("Worker AppDomain", null, ads);
            WrappedWorkerAppDomain = (WrapSession)WorkerAppDomain.CreateInstanceAndUnwrap(
                    Assembly.GetExecutingAssembly().FullName,
                    typeof(WrapSession).FullName
                );
#endif
        }
        public void Dispose()
        {
#if DEBUG && SAMEDOMAIN
#else
            WrappedWorkerAppDomain = null;
            AppDomain.Unload(WorkerAppDomain);
            WorkerAppDomain = null;
#endif
        }
    }
}
